package com.idroc.coelacanth.core.dht;

import org.jboss.netty.buffer.ChannelBuffer;

import com.idroc.coelacanth.core.membership.Endpoint;
import com.idroc.coelacanth.core.message.AbstractMessage;

/**
 * 端点转节点请求消息
 * @author yuyoo (yuyoo4j@163.com)
 * @date Dec 12, 2009 4:37:47 PM
 */
public class EndpointToNodeRequestMessage extends AbstractMessage {
	
	private int fromNo = -1;
	
	private Endpoint from = null;
	
	private Endpoint candidate = null;
	
	@Override
	public void decode(ChannelBuffer ib) { 
 
		fromNo = ib.readInt();
		
		from = new Endpoint();
		from.setHost(decodeString(ib));
		from.setPort(ib.readInt());
		
		candidate = new Endpoint();
		candidate.setHost(decodeString(ib));
		candidate.setPort(ib.readInt());
	}

	@Override
	public void encode(ChannelBuffer ob) { 
		
		ob.writeInt(fromNo);
 
		encodeString(ob, from.getHost());
		ob.writeInt(from.getPort());
		
		encodeString(ob, candidate.getHost());
		ob.writeInt(candidate.getPort());
	}

	public Endpoint getFrom() {
		return from;
	}

	public void setFrom(Endpoint from) {
		this.from = from;
	}

	public Endpoint getCandidate() {
		return candidate;
	}

	public void setCandidate(Endpoint candidate) {
		this.candidate = candidate;
	}
	
	public int getFromNo() {
		return fromNo;
	}

	public void setFromNo(int fromNo) {
		this.fromNo = fromNo;
	}

	@Override
	public String toString() {

		StringBuilder sb = new StringBuilder();
		sb.append("EndpointToNodeRequestMessage[");
		sb.append("sn:").append(this.getSN()).append(",");
		sb.append("from:").append(from).append(",");
		sb.append("candidate:").append(candidate);
		sb.append("]");
		return sb.toString();
	}
}
